// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import Qt.labs.particles 1.0

CharacterBase{
    width: 30
    height: 30
    color: "#00dd0000"

    property int type: 0

    property int moveInterval: 10
    property real shotIntervalThd: 0.01

    Particles {
        id: _particle
        anchors.centerIn: parent
        width: 1
        height: 1
        source: "./images/enemy1.png"
        lifeSpan: 1000
        count: 5
        angle: 0
        angleDeviation: 360
        velocity: 30
        velocityDeviation: 10
        visible: false
    }
    Timer{
        id: _particleTiemr
        running: false
        interval:500
        repeat: false
        onTriggered: {
            _particle.visible = false;
            isDestory = true;
        }
    }

    Image{
        id: _image
        anchors.centerIn: parent
        fillMode: Image.PreserveAspectCrop
        source: "./images/enemy1.png"

        Rectangle{
            id: _hitEffect
            anchors.fill: parent
            color: "#00ffffff"
            ColorAnimation{
                id: _hitAmin
                target: _hitEffect
                property: "color"
                from: "#66ffffff"
                to: "#00ffffff"
                duration: 100
            }
        }
    }

    onTypeChanged: {
        switch(type){
        case 0:
            _image.source = "./images/enemy1.png";
            _particle.source = "./images/enemy1.png";
            break;
        case 1:
            _image.source = "./images/enemy2.png";
            _particle.source = "./images/enemy2.png";
            break;
        case 2:
            _image.source = "./images/enemy3.png";
            _particle.source = "./images/enemy3.png";
            break;
        default:
            _image.source = "./images/enemy1.png";
            _particle.source = "./images/enemy1.png";
            break;
        }
        var i=0;
        i++;
//        _particleLoader.sourceComponent = _particleComponent
    }

    // キャラ特有の初期化処理
    function init(obj){
        moveInterval = moveInterval + ratio * -20 + 20;
        shotIntervalThd *= ratio;
        hitpoint = Math.ceil(hitpoint * ratio);
        type = obj._type;
    }

    property real moveLenX: 5
    property real moveLenY: 0
    property int moveCount: 0

    // 移動
    function move(){
        if((tickCount % (moveInterval)) === 0){
            if(moveLenY > 0){
                y += moveLenY;
            }else{
                x += moveLenX;
                moveCount++;
            }
            if(moveCount > 14){
                moveLenX *= -1;
                moveLenY = 15;
                moveCount = 0;
            }else{
                moveLenY = 0;
            }
        }
    }

    // 弾を撃つ？
    function getBullet(){
        shotCountInFrame--;
        if(shotCountInFrame < 0){
            return null;
        }else if(Math.random() < shotIntervalThd){
            return {"_qml": "EnemyBullet.qml"
                , "_charId": -1
                , "_x": x + width / 2
                , "_y": y
                , "_fieldWidth": fieldWidth
                , "_fieldHeight": fieldHeight
                , "_scorepoint": 100
                , "_hitpoint": 1
                , "_ratio": ratio
                , "_init":{}
            };
        }else{
            return null;
        }
    }

    // 弾があたったときの処理
    onHited: {
        _hitAmin.start();
    }
    //倒された
    onDestorying: {
        _particle.visible = true;
        _particleTiemr.start();
        isDestory = false;  //一旦消えるのを阻止
    }

}
